//AjaxServlet.java
package com.ylaihui.servlet;

import com.google.gson.Gson;
import com.ylaihui.json.Order;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lisimmy www.ylaihui.com
 * @create 2021-08-07
 */
public class AjaxServlet extends BaseServlet {

    protected void javaScriptAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ajax request...");
        Order order = new Order(1, "order1", 11.11);

        Gson gson = new Gson();

        String jsonString = gson.toJson(order);

        resp.getWriter().write(jsonString);

    }


    protected void jQueryAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQueryAjax request...");
        Order order = new Order(1, "order1", 11.11);

        Gson gson = new Gson();

        String jsonString = gson.toJson(order);

        resp.getWriter().write(jsonString);

    }

    protected void jQueryGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQueryGet request...");
        Order order = new Order(1, "order1", 11.11);

        Gson gson = new Gson();

        String jsonString = gson.toJson(order);

        resp.getWriter().write(jsonString);

    }


    protected void jQueryPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQueryPost request...");
        Order order = new Order(1, "order1", 11.11);

        Gson gson = new Gson();

        String jsonString = gson.toJson(order);

        resp.getWriter().write(jsonString);

    }


    protected void jQueryGetJSON(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQueryPost request...");
        Order order = new Order(1, "order1", 11.11);

        Gson gson = new Gson();

        String jsonString = gson.toJson(order);

        resp.getWriter().write(jsonString);

    }


    protected void jQuerySerialize(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jQuerySerialize request...");

        String name = req.getParameter("username");
        System.out.println(name);

        Order order = new Order(1, "order1", 11.11);

        Gson gson = new Gson();

        String jsonString = gson.toJson(order);

        resp.getWriter().write(jsonString);

    }




}
